﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;
using DAL;

namespace Triple7Service
{
    public class Tet2013Event : ITet2013Event
    {
        private static object objLock = new object();
        private static List<Tet2013TopScoreEntity> _ScoreTop = null;

        private static Thread _ThreadUpdateTop = null;

        private static List<Tet2013TopScoreEntity> GetScoreTop()
        {
            #region thread update top
            if (_ThreadUpdateTop == null || !_ThreadUpdateTop.IsAlive)
            {
                #region
                lock (objLock)
                {
                    if (_ThreadUpdateTop == null || !_ThreadUpdateTop.IsAlive)
                    {
                        _ThreadUpdateTop = new Thread(() =>
                        {
                            bool isStop = false;
                            do
                            {
                                try
                                {
                                    const int TOP_NUMBER = 10;
                                    var list = DB.HighLowGameInstance.uspEventTriple7Tet2013GetTop(TOP_NUMBER).ToList();
                                    List<Tet2013TopScoreEntity> result = new List<Tet2013TopScoreEntity>(TOP_NUMBER);
                                    foreach (var item in list)
                                    {
                                        result.Add(new Tet2013TopScoreEntity()
                                        {
                                            Id = item.Id,
                                            SessionId = item.SessionId,
                                            AccountId = item.AccountId,
                                            AuBet = item.AuBet,
                                            AuCurrent = item.AuCurrent,
                                            Round = item.Round,
                                            CollectAt = item.SessionCreatedDate.ToString("dd/MM/yyyy HH:mm:ss"),
                                            Score = item.Score
                                        });
                                    }
                                    _ScoreTop = result;
                                }
                                catch (Exception ex)
                                {
                                    isStop = true;
                                }
                                Thread.Sleep(60000);
                            } while (!isStop);
                        });
                        _ThreadUpdateTop.Start();
                    }
                }
                #endregion
            }

            if (_ScoreTop == null)
                return new List<Tet2013TopScoreEntity>();

            return _ScoreTop;
            #endregion
        }

        public List<Tet2013TopScoreEntity> GetTop()
        {
            return GetScoreTop();
        }
    }
}
